package com.buddy.india.buddy.party.man.go.money;

import android.Manifest;
import android.app.Activity;
import android.content.pm.PackageManager;
import android.os.Build;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import com.getcapacitor.JSObject;
import com.getcapacitor.PluginCall;
import com.google.gson.annotations.Until;

import java.util.ArrayList;

public class PermissionUtils extends Activity {
    private static PluginCall newCall = null;
    private boolean isPermissionRequested;



   private JSObject ret = new JSObject();
    public void requestPermission(PluginCall call) {
        newCall = call;
//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
//            if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
//                // Permission is not granted, request it
//
//                ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA}, 12);
//            } else {
//                // Permission is already granted, perform the camera related operation
//                // ...
//            }
//        }
        if (Build.VERSION.SDK_INT >= 23 && !isPermissionRequested) {

//            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
//                ret = context.checkSelfPermission(permission) == PackageManager.PERMISSION_GRANTED;
//            } else {
//                ret = PermissionChecker.checkSelfPermission(context, permission) == PermissionChecker.PERMISSION_GRANTED;
//            }

            isPermissionRequested = true;
            ArrayList<String> permissionsList = new ArrayList<>();
            String[] Permission={
                    Manifest.permission.READ_PHONE_STATE,Manifest.permission.CAMERA,Manifest.permission.READ_EXTERNAL_STORAGE,
                    Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.READ_CONTACTS,Manifest.permission.ACCESS_COARSE_LOCATION,
                    Manifest.permission.ACCESS_FINE_LOCATION,Manifest.permission.READ_SMS};

            ActivityCompat.requestPermissions(this, Permission, 1);
        }
    }


    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == 1) {
            boolean allGranted = true;
            // 判断是否所有的权限都已经授予了
            for (int grant : grantResults) {
                if (grant != PackageManager.PERMISSION_GRANTED) {
                    allGranted = false;
                    break;
                }
            }
            if (allGranted) {
                isPermissionRequested = true;
                ret.put("isAllRoot",true);

            } else {
                ret.put("isAllRoot",false);
            }
        }
    }


}
